package com.wyg.epdata.util;

import com.wyg.epdata.entity.vo.CountByDate;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
 * @Description: 注意： 1. 1 2 3要补0 比如 01
 * @Params: 2. i要判断是否越界
 * @return: 3. 特殊情况特殊处理就ok，比如year跨越，直接在后面补01-01
 **/

/**
 * @Author: WangYongGan
 * @Date: 2022/1/28 19:10
 * @Description:
 */
@Slf4j
public class DateAndDataUtil {
    public static int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static void fillNullBy0Day(String begin, String end, List<CountByDate> countByDateList) {
        countByDateList.sort(Comparator.comparing(CountByDate::getDate));
        List<CountByDate> res = new ArrayList<>();
        int i = 0;
        while (begin.compareTo(end) < 0) {
            if (i < countByDateList.size() && countByDateList.get(i).getDate().equals(begin))
                i++;
            else {
                CountByDate countByDate = new CountByDate();
                countByDate.setDate(begin);
                countByDate.setSum(0);
                res.add(countByDate);
            }
            begin = getNextDay(begin);
        }
        countByDateList.addAll(res);
        countByDateList.sort(Comparator.comparing(CountByDate::getDate));
    }

    public static String getNextDay(String date) {
        int year = Integer.parseInt(date.split("-")[0]);
        int month = Integer.parseInt(date.split("-")[1]);
        int day = Integer.parseInt(date.split("-")[2]);
        if (month == 2 && isEven(year)) {
            if (day == 28)
                return date.substring(0, date.length() - 1) + "9";
        }
        if (day >= days[month - 1]) {
            if (month == 12)
                return year + 1 + "-01-01";
            else return year + "-" + getBase10(month + 1) + "-01";
        }
        return year + "-" + getBase10(month) + "-" + getBase10(day + 1);
    }

    public static boolean isEven(int year) {
        return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
    }

    public static String getBase10(int n) {
        if (n < 10)
            return "0" + n;
        return String.valueOf(n);
    }

    public static void main(String[] args) {
        System.out.println(Integer.parseInt("01"));
    }
}
